//
//  NewHomeSearchView.m
//  NewPro
//
//  Created by RYX on 2019/4/25.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "NewHomeSearchView.h"
#import "MessageCenterViewController.h"
#import "MineStore.h"
#import "UserModel.h"
#import "HGOrientationLabel.h"
#import "HomeList.h"
#import "CBAutoScrollLabel.h"
#import "HomeSearchVC.h"
#import "CYYServiceViewController.h"

@interface NewHomeSearchView ()

@property (nonatomic,weak) UIButton *leftBtn;
@property (nonatomic,weak) UIButton *rightBtn;
@property (nonatomic,weak) UIView *bottomLine;
@property (nonatomic,weak) UILabel *numNewsLa;
@property (nonatomic,weak) UILabel *notiTitleLa;
@property (nonatomic,weak) CBAutoScrollLabel *newsLa;
@property (nonatomic,weak) UIView *searchView;
@property (nonatomic,weak) UIView *newsBgView;
@end

@implementation NewHomeSearchView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self setUI];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setRed:) name:@"SETMINERED" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setRed:) name:@"headerTopNewsNum" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:@"REALNAMEING" object:nil];
    }
    
    return self;
}

- (void)addToastView{
    
    self.newsView.hidden = NO;
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setRed:(NSNotification *)noti{
    
    NSString *str = noti.userInfo[@"info"];
    
    self.numNewsLa.text = str;
    self.numNewsLa.alpha = 1;
    
    if ([str integerValue] == 0) {
        
        self.numNewsLa.alpha = 0;
    }
}

- (void)setUI{
    
    UIView *searchView = [[UIView alloc] init];
    self.searchView = searchView;
    [self addSubview:searchView];
    
    UIButton *leftBtn = [[UIButton alloc]init];
    [leftBtn setImage:[UIImage imageNamed:@"客服"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(toServiceOnline) forControlEvents:UIControlEventTouchUpInside];
    [searchView addSubview:leftBtn];
    self.leftBtn = leftBtn;
    
    UIButton *rightBtn = [[UIButton alloc]init];
    [rightBtn setImage:[UIImage imageNamed:@"首页消息中心"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(toMessage) forControlEvents:UIControlEventTouchUpInside];
    [searchView addSubview:rightBtn];
    self.rightBtn = rightBtn;
    
    UILabel *numNewsLa = [[UILabel alloc] init];
    numNewsLa.layer.cornerRadius = 10;
    numNewsLa.backgroundColor = UIColor.redColor;
    numNewsLa.textColor = UIColor.whiteColor;
    numNewsLa.text = @"0";
    numNewsLa.font = [UIFont systemFontOfSize:10];
    numNewsLa.layer.masksToBounds = YES;
    numNewsLa.textAlignment = NSTextAlignmentCenter;
    numNewsLa.adjustsFontSizeToFitWidth = YES;
    numNewsLa.alpha = 0;
    self.numNewsLa = numNewsLa;
    [searchView addSubview:numNewsLa];
    
    self.search = [[UITextField alloc]init];
    _search.userInteractionEnabled = NO;
    _search.layer.cornerRadius = 17;
    _search.backgroundColor = [UIColor colorWithWhite:1 alpha:0.3];
    _search.textColor = [UIColor whiteColor];
    _search.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:12];
    _search.placeholder = @"畅享合伙人";
    [_search setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    UIView *imag = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 35, 35)];
    UIImageView *searchImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 15, 15)];
    searchImage.image = [UIImage imageNamed:@"白色搜索"];
    [imag addSubview:searchImage];
    _search.leftView = imag;
    _search.leftViewMode = UITextFieldViewModeAlways;
    [searchView addSubview:_search];
    
    UIButton *searchbtn = [[UIButton alloc] initWithFrame:_search.frame];
    [searchbtn addTarget:self action:@selector(onSearchBtn:) forControlEvents:UIControlEventTouchUpInside];
    [searchView addSubview:searchbtn];
    
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = [UIColor colorWithHexString:@"E0DFDF"];
    bottomLine.alpha = 0;
    [self addSubview:bottomLine];
    self.bottomLine = bottomLine;

    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(searchView).offset(14);
        make.centerY.equalTo(searchView);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(23);
    }];
    
    [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(searchView).offset(-15);
        make.centerY.equalTo(searchView);
        make.width.height.mas_equalTo(24);
    }];
    
    [_search mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftBtn.mas_right).offset(11);
        make.top.bottom.equalTo(searchView);
        make.right.equalTo(rightBtn.mas_left).offset(-11);
    }];
    
    [searchbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftBtn.mas_right).offset(11);
        make.top.bottom.equalTo(searchView);
        make.right.equalTo(rightBtn.mas_left).offset(-11);
    }];
    
    [numNewsLa mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(rightBtn.mas_top).offset(-5);
        make.left.equalTo(rightBtn.mas_right).offset(-15);
        make.width.height.mas_equalTo(20);
    }];

    UIView *newsView = [[UIView alloc] init];
    self.newsView = newsView;
    [self addSubview:newsView];
    
    UIView *newsBgView = [[UIView alloc] init];
    self.newsBgView = newsBgView;
    newsView.hidden = YES;
    newsBgView.backgroundColor = [UIColor colorWithRed:0.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:0.5];
    [newsView addSubview:newsBgView];
    [newsBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.bottom.right.equalTo(newsView);
        make.top.equalTo(newsView);
    }];
    
    UIImageView *labaIm = [[UIImageView alloc] init];
    labaIm.image = [UIImage imageNamed:@"labaIm"];
    [newsBgView addSubview:labaIm];
    [labaIm mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(newsBgView);
        make.width.height.mas_equalTo(12);
        make.left.equalTo(newsBgView.mas_left).offset(14);
    }];
    
    UIButton *imgBtn = [[UIButton alloc] init];
    [imgBtn setImage:[UIImage imageNamed:@"closeBtn"] forState:UIControlStateNormal];
    [newsBgView addSubview:imgBtn];
    [imgBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(newsBgView).offset(-14);
        make.centerY.equalTo(newsBgView);
        make.width.height.mas_equalTo(12);
        
    }];
    
    CBAutoScrollLabel *newsLa = [[CBAutoScrollLabel alloc] init];
    newsLa.textColor = [UIColor whiteColor];
    newsLa.backgroundColor = [UIColor clearColor];
    newsLa.labelSpacing = 30;
    newsLa.pauseInterval = 1.7;
    newsLa.scrollSpeed = 30;
    newsLa.textAlignment = NSTextAlignmentLeft;
    newsLa.fadeLength = 12.f;
    newsLa.font = [UIFont systemFontOfSize:12];
    newsLa.scrollDirection = CBAutoScrollDirectionLeft;
    [newsLa observeApplicationNotifications];
    [newsBgView addSubview:newsLa];
    self.newsLa = newsLa;
    
    [newsLa mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.equalTo(newsBgView);
        make.left.equalTo(labaIm.mas_right).offset(2);
        make.right.equalTo(imgBtn.mas_left).offset(-5);
    }];
    
    UIButton *closeBtn = [[UIButton alloc] init];
    [newsView addSubview:closeBtn];
    [closeBtn addTarget:self action:@selector(onCloseBtn) forControlEvents:UIControlEventTouchUpInside];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.right.equalTo(newsBgView);
        make.width.mas_equalTo(75);
        
    }];
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    self.bottomLine.x = 0;
    self.bottomLine.y = self.height - 0.5;
    self.bottomLine.height = 0.5;
    self.bottomLine.width = ScreenWidth;
    
    self.searchView.x = 0;
    self.searchView.y = self.height - 34 - 10;
    self.searchView.width = ScreenWidth;
    self.searchView.height = 34;
    
    self.newsView.x = 0;
    self.newsView.y = STATUS_BAR_HEIGHT;
    self.newsView.width = ScreenWidth;
    self.newsView.height = 20;
}

- (void)onCloseBtn{
    
    //保存关闭的通知
    NSUserDefaults *de = [NSUserDefaults standardUserDefaults];
    NSString *systemContent = [de objectForKey:@"systemContent"];
    NSString *systemId = [de objectForKey:@"systemId"];
    [de setObject:systemContent forKey:@"closeSystemContent"];
    [de setObject:systemId forKey:@"closeSystemId"];
    
    [HomeList sharedInstance].closeToast = YES;
    
    self.newsView.hidden = YES;
    
    if (self.closeBlock != nil) {
        
        self.closeBlock();
    }
    
}

- (void)bgClik{
    
    [self.search resignFirstResponder];
}
- (void)toMessage{
    [[AppDelegate sharedAppDelegate] pushViewController:[[MessageCenterViewController alloc] init]];
}

- (void)setAalpha:(CGFloat)aalpha{
    
    if (aalpha >= 1) {
        
        UIView *imag = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 35, 35)];
        UIImageView *searchImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 15, 15)];
        searchImage.image = [UIImage imageNamed:@"白色搜索2"];
        [imag addSubview:searchImage];
        _search.leftView = imag;
        
        [self.leftBtn setImage:[UIImage imageNamed:@"客服2"] forState:UIControlStateNormal];
        [self.rightBtn setImage:[UIImage imageNamed:@"首页消息中心2"] forState:UIControlStateNormal];
        self.search.layer.borderUIColor = [UIColor colorWithHexString:@"D0D0D0"];
        self.search.layer.borderWidth = 0.5;
        self.bottomLine.alpha = 1;
        [self.search setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        [self getCurrentViewController].navigationController.navigationBar.barStyle =  UIBarStyleDefault;
        
    }else{
        
        UIView *imag = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 35, 35)];
        UIImageView *searchImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 15, 15)];
        searchImage.image = [UIImage imageNamed:@"白色搜索"];
        [imag addSubview:searchImage];
        _search.leftView = imag;
        
        [self.leftBtn setImage:[UIImage imageNamed:@"客服"] forState:UIControlStateNormal];
        [self.rightBtn setImage:[UIImage imageNamed:@"首页消息中心"] forState:UIControlStateNormal];
        self.search.layer.borderUIColor = [UIColor whiteColor];
        self.search.layer.borderWidth = 0;
        [self.search setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        self.bottomLine.alpha = 0;
        [self getCurrentViewController].navigationController.navigationBar.barStyle =  UIBarStyleBlackOpaque;
    }
}

- (void)refresh{
    
    MineStore *store = [[MineStore alloc] init];
    
    [store getUserInfoWithCustomId:[UserDefaultsManager sharedInstance].user_id Success:^(UserModel *model) {
        
        NSString *str = [NSString stringWithFormat:@"%@",model.readnum];
        
        if (![str isEqualToString:@"0"] && ![str isEqualToString:@"(null)"]) {
            
            
            self.numNewsLa.text = str;
            self.numNewsLa.alpha = 1;
        }else{
            
            self.numNewsLa.text = @"0";
            self.numNewsLa.alpha = 0;
        }
        
        
    } Failure:^(NSError *error) {
        
    }];
}

- (void)setNotiTitle:(NSString *)notiTitle{
    
    self.notiTitleLa.text = notiTitle;
}
- (void)setNotiStr:(NSString *)notiStr{
    
    self.newsLa.text = notiStr;
    self.newsBgView.hidden =NO;
}

- (void)returnCloseBlock:(closeBlock)block{
    
    self.closeBlock = block;
}

#pragma mark - 搜索
- (void)onSearchBtn:(UIButton *)btn{
    [[AppDelegate sharedAppDelegate] pushViewController:[[HomeSearchVC alloc]init]];
}

#pragma mark - 在线客服
- (void)toServiceOnline{
    
    CYYServiceViewController *vc = [[CYYServiceViewController alloc] init];
    [vc setHidesBottomBarWhenPushed:YES];
    [[self getCurrentViewController].navigationController pushViewController:vc animated:YES];
}
@end
